package com.example.dataservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dataservice.entity.Admin;
import com.example.dataservice.entity.User;
import com.example.dataservice.mapper.AdminMapper;
import com.example.dataservice.service.impl.AdminServiceImpl;
import com.example.dataservice.service.impl.UserServiceImpl;
import com.example.dataservice.utils.Result;
import com.example.dataservice.utils.pageParam;
import com.example.dataservice.vo.LoginVo;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-01-12
 */
@RestController
@RequestMapping("/api/user")
public class UserController {

    @Resource
    private UserServiceImpl userService;
    @Resource
    private AdminServiceImpl adminService;
    @PostMapping("/login")
    public Result<LoginVo> login(@RequestBody Map<String ,String> map){
        String userName = map.get("username");
        String password = map.get("password");
        String s = DigestUtils.md5DigestAsHex((password).getBytes());
        LambdaQueryWrapper<Admin> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Admin::getUserName,userName);
        Admin one = adminService.getOne(queryWrapper);
        if(one==null)return Result.fail("用户不存在");
        String password1 = one.getPassword();
        if(!password.equals(password1))return Result.fail("密码错误");
        String token = UUID.randomUUID().toString();
        LoginVo res = new LoginVo(userName,s,token);
        return Result.success(res);
    }
    @PostMapping("/list")
    public Result<List<User>> pageSelect(@RequestBody pageParam param){
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        if(param.getT1()!=null&& !param.getT1().isEmpty())queryWrapper.eq(User::getUserName,param.getT1());
        if(param.getT2()!=null&& !param.getT2().isEmpty())queryWrapper.eq(User::getPhoneNumber,param.getT2());
        Page<User> pageInfo=new Page<>(param.getPage(),param.getLimit());
        Page<User> page1 = userService.page(pageInfo,queryWrapper);
        return  Result.success(page1.getRecords(),page1.getTotal());
    }

    @PostMapping("/save")
    public Result<String> save(@RequestBody User user){
        if(user.getId()==null){
            user.setCreateTime(LocalDate.now());
            user.setUpdateTime(LocalDate.now());
            userService.save(user);
            return Result.success("添加成功");
        }else {
            LambdaUpdateWrapper<User> updateWrapper=new LambdaUpdateWrapper<>();
            updateWrapper.eq(User::getId,user.getId());
            userService.update(user,updateWrapper);
            return Result.success("修改成功");
        }
    }

    @DeleteMapping("/delete/{id}")
    public Result<String > delete(@PathVariable("id")String id){
        userService.removeById(id);
        return Result.success("删除成功");
    }
    
    @PostMapping("/edit")
    public Result<String> edit(@RequestBody Admin admin){
        LambdaQueryWrapper<Admin> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(Admin::getUserName,admin.getUserName());
        boolean isSuccess = adminService.update(admin,lambdaQueryWrapper);
        if(isSuccess)
            return Result.success("修改成功");
        return Result.fail("修改失败");
    }

}
